package cn.shentianlan.service.impl;

import cn.shentianlan.criteria.StudentCriteria;
import cn.shentianlan.domain.dto.ClassDTO;
import cn.shentianlan.domain.dto.StudentDTO;
import cn.shentianlan.domain.entity.Clazz;
import cn.shentianlan.domain.entity.Student;
import cn.shentianlan.domain.entity.User;
import cn.shentianlan.domain.enums.GenderCode;
import cn.shentianlan.domain.enums.RoleNameCode;
import cn.shentianlan.mapper.StudentMapper;
import cn.shentianlan.mapper.UserMapper;
import cn.shentianlan.service.ClassService;
import cn.shentianlan.service.StudentService;
import cn.shentianlan.service.UserService;
import cn.shentianlan.utils.BaseUtils;
import cn.shentianlan.utils.DataUtils;
import cn.shentianlan.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 陈俊源
 * @Description TODO
 * @Date 2021/4/11
 * @Version 1.0
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ClassService classService;

    @Transactional
    @Override
    public Result saveOrUpdate(StudentDTO studentDTO){
        Result result = checkStudent(studentDTO);
        if(!Result.isSuccess(result)){
            return result;
        }
        Student student = BaseUtils.copyProperties(studentDTO, Student.class);
        User user = BaseUtils.copyProperties(studentDTO, User.class);
        if (studentDTO.getId()==null){
            Long id = DataUtils.assignId();
            user.setId(id);
            student.setId(id);
            userMapper.saveUser(user);
            studentMapper.saveStudent(student);
        }else{
            userMapper.updateUser(user);
            studentMapper.updateStudent(student);
        }
        return Result.success(student.getId());
    }

    @Override
    public Result getStudentById(Long id) {
        if (id == null){
            return Result.fail("id为空，无法获取学员信息");
        }
        Student student = studentMapper.getStudentById(id);
        if(student == null){
            return Result.fail("无法获取该id对应的学员信息");
        }
        return Result.success(student);
    }

    @Override
    public Result findStudentByCriteria(StudentCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(),criteria.getPageSize(),false);
        List<Student> studentList = studentMapper.findStudentByCriteria(criteria);
        Integer count = studentMapper.getCount(criteria);
        List<StudentDTO> studentDTOList = new ArrayList<StudentDTO>();
        for (Student student : studentList ) {
            StudentDTO studentDTO = bulidStudentDTO(student);
            studentDTOList.add(studentDTO);
        }
        PageInfo<StudentDTO> pageInfo = new PageInfo<StudentDTO>(studentDTOList);
        pageInfo.setTotal(count.longValue());
        return Result.success(pageInfo);
    }

    public StudentDTO bulidStudentDTO(Student student){
        StudentDTO studentDTO = BaseUtils.copyProperties(student,StudentDTO.class);
        studentDTO.setRoleNameDec(RoleNameCode.getValue(studentDTO.getRoleName()));
        studentDTO.setGenderDec(GenderCode.getValue(studentDTO.getGender()));
        if (student.getClassId()!=null){
            Result classResult = classService.getClassById(student.getClassId());
            Clazz clazz = (Clazz) classResult.getData();
            studentDTO.setClassName(clazz.getClassName());
        }
        return studentDTO;
    }

    //todo 后期添加信息校验
    private Result checkStudent(StudentDTO studentDTO) {
        return Result.success();
    }
}
